<?php
	/**
	 * 模型基类
	 * Enter description here ...
	 * @author cui
	 *
	 */
    class model extends base
    {
        /**
         * 数据库单例
         * @var unknown_type
         */
        public $db;

        /**
         * 数据列
         * @var unknown_type
         */
        public $field = '*';

        /**
         * 某条数据的ID
         * @var unknown_type
         */
        public $where;

        /**
         * 数据库表名称
         * @var unknown_type
         */
        public $table_name;

        /**
         * 排序
         * @var unknown_type
         */
        public $order_by;

        /**
         * 最后插入的ID
         */
        public $last_insert_id;

        public function __construct()
        {
            $this->db = db::getInstance();
        }

        public function set_order_by($field, $order_type = 'DESC')
        {
            $this->order_by = "order by `$field` $order_type";
        }

        /**
         * 用户设置查询的数据列
         * @param unknown_type $field
         */
        public function set_field(Array $field)
        {
            $field_tmp = '';

            foreach ($field AS $v)
            {
                $field_tmp .= "`{$v}`,";
            }

            $this->field = substr($field_tmp, 0, -1);
        }

        /**
         * 生成in查询
         * @param unknown_type $str
         * @param unknown_type $field
         */
        public function set_in($str, $field)
        {
            $this->where = ' where ' . $field . ' in(' . $str . ')';
        }

        /**
         * 在SQL中游IN和其他条件语句的情况下使用
         * 临时方法，用于解决SET_WHERE和SET_IN的不共同问题
         * @param unknown_type $field
         * @param unknown_type $in_str
         * @param unknown_type $where_data
         */
        public function set_in_and_where($field, $in_str, $where_data)
        {
        	$this->where = "where `{$field}` in ({$in_str})";

        	foreach ($where_data AS $k => $v)
        	{
        		$this->where .= " AND `{$k}` = '{$v}'";
        	}

        	return $this->get_list();
        }

        /**
         * 生成条件语句，用于区别更新或删除
         * @param $id
         */
        public function set_where($id, $field = '')
        {
            if (is_array($id) === TRUE)
            {
                //如果$id是一个数组，就表示有多个WHERE条件
                $where = '';

                foreach ($id AS $k => $v)
                {
                    $where .= "`{$k}` = '{$v}' AND";
                }

                $where = substr($where, 0, -4);

                $this->where = $where;
            }
            else
            {
                if ($field != '')
                {
                    //如果$field不为空，那么用$field = $id组织成WHERE语句
                    $this->where = "`{$field}` = '{$id}'";
                }
                else
                {
                    //用id做列用$id做值组织WHERE语句
                    $this->where = "`id` = '{$id}'";
                }
            }

            if (empty($this->where) === FALSE)
            {
                $this->where = ' where ' . $this->where;
            }
        }

        /**
         * 设置表名
         * @param $name
         */
        public function set_table_name($name)
        {
            $this->table_name = $name;
        }

        /**
         * 获取一条记录
         * @param unknown_type $id
         * @param unknown_type $field
         */
        public function load($id, $field = '')
        {
            $this->set_where($id, $field);

            $sql = "select {$this->field} from `{$this->table_name}` {$this->where}";

            return $this->db->query($sql)->fetch_one()->query_result;
        }

        /**
         * 保存数据
         */
        public function save($data)
        {
            if (empty($this->where) === FALSE)
            {
                //更新
                $set = '';

                foreach($data AS $k => $v)
                {
                    $set .= "`{$k}` = '{$v}',";
                }

                $set = substr($set, 0, -1);

                $sql = "update `{$this->table_name}` set {$set} {$this->where}";
            }
            else
            {
                //新增
                $field = '';
                $value = '';

                foreach ($data AS $k => $v)
                {
                    $field .= "`{$k}`,";
                    $value .= "'{$v}',";
                }

                $field = substr($field, 0, -1);
                $value = substr($value, 0, -1);

                $sql = "insert into `{$this->table_name}`({$field}) values({$value})";
            }

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        /**
         * 删除条目
         * @param unknown_type $id
         * @param unknown_type $field
         */
        public function delete($id, $field = '')
        {
            $this->set_where($id, $field);

            $sql = "delete from `{$this->table_name}` {$this->where}";

            $this->db->query($sql);

            if ($this->db->query_result)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        /**
         * 获取列表
         * @param $limit
         * @param $offset
         */
        public function get_list($limit = 0, $offset = 0)
        {
            if ($limit == 0)
            {
                $limit_str = '';
            }
            else
            {
                $limit_str = "limit {$offset}, {$limit}";
            }

            $sql = "select {$this->field} from {$this->table_name} {$this->where} {$this->order_by} {$limit_str}";

            return $this->db->query($sql)->fetch_all()->query_result;
        }

        public function get_count()
        {
            $sql = "select count(*) from `{$this->table_name}` {$this->where}";

            return $this->db->query($sql)->getCount()->query_result;
        }

        /**
         * 返回最后一次插入的ID
         */
        public function last_insert_id()
        {
            return $this->db->last_insert_id();
        }

        /**
         * 获取模型数据
         * @param unknown_type $id	记录id
         * @param unknown_type $mid	模型id
         */
        public function get_profile($id, $mid)
        {
            require_once MODEL_PATH . 'admin/model.php';
            $model = new model();

            $model->set_field(array('table_name'));
            $res = $model->load($mid);

            $res = $model->load_model($res['table_name'], $id);

            $profile = array();

            foreach ($res AS $v)
            {
                $profile[$v['key']] = $v['value'];
            }

            unset($model);
            unset($res);

            return $profile;
        }
    }